package org.lib.mysqlhello.tcache.try1;

import java.util.Objects;

import org.lib.mysqlhello.common.ResultVO;
import org.lib.mysqlhello.tcache.try1.dto.AddDTO;
import org.lib.mysqlhello.tcache.try1.dto.UpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * Book接口
 * @author ben
 * @date 2021-08-26 11:10:07 CST
 */
@RestController
@RequestMapping(value="/try1/book")
@Slf4j
public class BookController {

	/**
	 * Book服务
	 */
	@Autowired
	private BookService bookService;
	
	@PostMapping(value="/add")
	public ResultVO<Book> add(@RequestBody AddDTO dto) {
		log.info("add Book");
		Book book = bookService.add(dto);
		if (Objects.nonNull(book)) {
			return ResultVO.getSuccess(book);
		} else {
			return ResultVO.getFailed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "亲，添加失败咯", null);
		}
	}
	
	@PostMapping(value="/del")
	public ResultVO<Boolean> del(@RequestParam Long id) {
		log.info("del Book");
		if (Objects.isNull(id)) {
			return ResultVO.getFailed(HttpStatus.BAD_REQUEST.value(), "亲，参数id缺失", null);
		}
		return ResultVO.getSuccess(bookService.del(id));
	}
	
	@PostMapping(value="/update")
	public ResultVO<Book> update(@RequestBody UpdateDTO dto) {
		log.info("update Book");
		Book book = bookService.update(dto);
		if (Objects.nonNull(book)) {
			return ResultVO.getSuccess(book);
		} else {
			return ResultVO.getFailed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "亲，更新失败咯", null);
		}
	}
	
	@GetMapping(value="/findById")
	public ResultVO<Book> findById(@RequestParam Long id) {
		log.info("findById Book");
		Book book = bookService.findById(id);
		if (Objects.nonNull(book)) {
			return ResultVO.getSuccess(book);
		} else {
			return ResultVO.getFailed(400, "亲，没有记录哦", null);
		}
	}
	
}
